package com.woniuxy.controller;

import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacManagerService;
import com.woniuxy.utils.JWTUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags="登录验证")
@RestController
public class LoginController {
    @Autowired
    RbacManagerService rbacManagerService;
    @ApiOperation("登录功能")
    @RequestMapping("/login")
    public ResponseEntity login(String username,String password){
        RbacManager rbacManager = rbacManagerService.login(username, password);
        if (!StringUtils.isEmpty(rbacManager)){
            //就说明查出了用户数据
            System.out.println("查询"+rbacManager);
            String token =  JWTUtils.sign(username, password);
            //将token封装到里面
            rbacManager.setToken(token);
            return new ResponseEntity<>(200,"登录成功",rbacManager);
        }else {
            return ResponseEntity.FAIL;
        }
    }
    @ApiOperation("认证异常返回")
    @CrossOrigin
    @RequestMapping("/401")
    public ResponseEntity response401(){
        return new ResponseEntity(401,"认证异常！",null);
    }
}
